package com.chis.coolplat;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * @author wlj
 * @version 2017年08月10日
 * @Description ReportUtils
 * @Company zwx
 */
public class ParseReportUtils {

	/**
	 *
	 * @param f f
	 * @param scale scale
	 * @return 结果
	 */
	private static double converDoub(double f, int scale) {
		BigDecimal bg = new BigDecimal(f);
		double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		return f1;
	}

	/**
	 *
	 * @param l l
	 * @return 结果
	 */
	public static String stampTodate(long l) {
		Date date = new Date(l);
		date = changeTimeZone(date, TimeZone.getTimeZone("Asia/Shanghai"), TimeZone.getTimeZone("GMT"));
		String format = DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
		return format;
	}

	/**
	 *
	 * @param date date
	 * @return 结果
	 * @throws ParseException 异常
	 */
	public static long dateToStamp(String date) throws ParseException {
		Date parseDate = DateUtils.parseDate(date, new String[] { "yyyy-MM-dd HH:mm:ss" });
		parseDate = changeTimeZone(parseDate, TimeZone.getTimeZone("GMT"), TimeZone.getTimeZone("Asia/Shanghai"));
		return parseDate.getTime();
	}

	/** 
	 * 获取更改时区后的日期 
	 * @param date 日期 
	 * @param oldZone 旧时区对象 
	 * @param newZone 新时区对象 
	 * @return 日期 
	 */
	public static Date changeTimeZone(Date date, TimeZone oldZone, TimeZone newZone) {
		Date dateTmp = null;
		if (date != null) {
			int timeOffset = oldZone.getRawOffset() - newZone.getRawOffset();
			dateTmp = new Date(date.getTime() - timeOffset);
		}
		return dateTmp;
	}

	/**
	 * 16进制字节数组转字符串
	 * @param src src
	 * @return 结果
	 */
	public static String bytesToHexString(byte[] src) {
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString().toUpperCase();
	}

	/**
	 *
	 * @return 结果
	 */
	public static String getCurrentDate5() {
		Calendar c = Calendar.getInstance();
		c.add(13, 0);
		return c.get(1) + "-" + (c.get(2) + 1) + "-" + c.get(5) + " " + c.get(11) + ":" + c.get(12) + ":" + c.get(13);
	}

	/**
	 * 16进制字符串转字符串
	 * @param s s
	 * @return 结果
	 */
	public static String hexStringToString(String s) {
		if (s == null || s.equals("")) {
			return null;
		}
		s = s.replace(" ", "");
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {
			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		try {
			s = new String(baKeyword, "UTF-8");
			new String();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}

	/**
	 * @param s 入参
	 * @return 返回值
	 */
	public static String stringToHexString(String s) {
		String str = "";
		for (int i = 0; i < s.length(); i++) {
			int ch = (int) s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = str + s4;
		}
		return str;
	}

	/**
	 *  获取16进制字节数组时间
	 * @return 结果
	 * @throws Exception 异常
	 */
	public static byte[] getCurrTimeByteArr() throws Exception {
		String hexString = Integer.toHexString(new Long(ParseReportUtils.dateToStamp(getCurrentDate5()) / 1000).intValue()).toUpperCase();
		if (hexString.length() == 8) {
			hexString = hexString.substring(6, 8) + hexString.substring(4, 6) + hexString.substring(2, 4) + hexString.substring(0, 2);
		}
		byte[] hexStringToBytes = hexStringToBytes(hexString);
		return hexStringToBytes;
	}

	/**
	 * Convert hex string to byte[] 16进制字符串 转字节数组
	 * 
	 * @param hexString
	 *            the hex string
	 * @return byte[]
	 */
	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		hexString = hexString.toUpperCase();
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] d = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
		}
		return d;
	}

	/**
	 * Convert char to byte
	 * 
	 * @param c
	 *            char
	 * @return byte
	 */
	private static byte charToByte(char c) {
		return (byte) "0123456789ABCDEF".indexOf(c);
	}

	/**
	 * 判断是否是16进制数
	 * @param strHex strHex
	 * @return 结果
	 */
	public static boolean IsHex(String strHex) {
		int i = 0;
		if (strHex.length() > 2) {
			if (strHex.charAt(0) == '0' && (strHex.charAt(1) == 'X' || strHex.charAt(1) == 'x')) {
				i = 2;
			}
		}
		for (; i < strHex.length(); ++i) {
			char ch = strHex.charAt(i);
			if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || (ch >= 'a' && ch <= 'f'))
				continue;
			return false;
		}
		return true;
	}

	/**
	 * 计算16进制对应的数值
	 * @param ch ch
	 * @return 结果
	 * @throws Exception 异常
	 */
	public static int GetHex(char ch) throws Exception {
		if (ch >= '0' && ch <= '9')
			return (int) (ch - '0');
		if (ch >= 'a' && ch <= 'f')
			return (int) (ch - 'a' + 10);
		if (ch >= 'A' && ch <= 'F')
			return (int) (ch - 'A' + 10);
		throw new Exception("error param");
	}

	/**
	 *  计算幂
	 * @param nValue nValue
	 * @param nCount nCount
	 * @return 结果
	 * @throws Exception 异常
	 */
	public static int GetPower(int nValue, int nCount) throws Exception {
		if (nCount < 0)
			throw new Exception("nCount can't small than 1!");
		if (nCount == 0)
			return 1;
		int nSum = 1;
		for (int i = 0; i < nCount; ++i) {
			nSum = nSum * nValue;
		}
		return nSum;
	}

	/**
	 * 16进制转2进制
	 * @param hexString hexString
	 * @author xt
	 * @return 结果
	 */
	public static String hexString2binaryString(String hexString) {
		if (hexString == null || hexString.length() % 2 != 0)
			return null;
		String bString = "";
		String tmp = "";
		for (int i = 0; i < hexString.length(); i++) {
			tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
			bString += tmp.substring(tmp.length() - 4);
		}
		return bString;
	}

	/**
	 * 2进制转16进制
	 * @param bString bString
	 * @author xt
	 * @return 结果
	 */
	public static String binaryString2hexString(String bString) {
		if (bString == null || bString.equals("") || bString.length() % 8 != 0)
			return null;
		StringBuffer tmp = new StringBuffer();
		int iTmp = 0;
		for (int i = 0; i < bString.length(); i += 4) {
			iTmp = 0;
			for (int j = 0; j < 4; j++) {
				iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
			}
			tmp.append(Integer.toHexString(iTmp));
		}
		return tmp.toString();
	}


	/**
	 * 高低位16进制转换
	 * @param content 入参
	 * @return 返回值
	 */
	public static String LowTransferHigh(String content){
		String [] strs = new String[content.length()/2];
		for(int i = 0;i < strs.length ; i++){
			strs[i] = content.substring(i*2,i*2+2);
		}
		List<String> strings = Arrays.asList(strs);
		Collections.reverse(strings);
		StringBuffer buffer = new StringBuffer();
		for(String s:strings){
			buffer.append(s);
		}
		return buffer.toString();
	}

}
